<script setup>
import {ref} from "vue";
import { open } from '@tauri-apps/plugin-dialog';
import { Command } from '@tauri-apps/plugin-shell';

const fileUrl = ref('')
async function chooseFile() {
  const file = await open({
    multiple: false,
    directory: false,
  })
  if(file) {
    fileUrl.value = file
  }
}

const jsonText = ref('')
async function readDocx() {
  const command = Command.sidecar('binaries/docx_to_json', ['--file', fileUrl.value]);
  // debugger
  // const output = await command.execute();
  // debugger

  command.execute().catch((e, a) => {
    debugger
  }).then(res => {
    debugger
  }).finally(e => {
    debugger
  })
  if (output.code !== 0) {
    throw new Error(`Python进程执行失败: ${output.stderr}`);
  }

  // 解析Python程序的输出
  const result = JSON.parse(output.stdout);

  if (!result.success) {
    throw new Error(result.error || '未知错误');
  }

  console.log('DOCX转换成功，段落数量:', result.data?.paragraphs?.length || 0);
  jsonText.value = result.data;
}
</script>

<template>
  <div class="view-box fit">
    <div class="row items-center">
      <el-input
          v-model="fileUrl"
          class="col-grow"
          placeholder="文件路径"
      />
      <el-button type="primary" class="q-ml-sm" @click="chooseFile">选择文件</el-button>
    </div>
    <div class="row items-center">
      <el-button type="primary" @click="readDocx">读取</el-button>
    </div>
    <div class="text">{{jsonText}}</div>
  </div>
</template>

<style scoped>

</style>
